package com.ruoyi.chat.mapper;

import com.ruoyi.chat.domain.ChatRecords;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author WaKy
 * @version 1.0
 */
@Mapper
public interface DeepSeekMapper {
    /**
     * 保存用户问题并返回插入记录的主键
     *
     * @param content 用户问题内容
     * @return 插入记录的主键值
     */
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into chat_records (user_question, timestamp, create_time, is_deleted) values (#{userQuestion}, #{timestamp}, #{createTime}, #{isDeleted})")
    int saveUserQuestion(ChatRecords content);

    /**
     * 保存 AI 回复
     *
     * @param response AI 回复内容
     */
    @Update("update chat_records set ai_response = #{response} where id = #{recordId}")
    void saveAiResponse(@Param("response") String response, @Param("recordId") Long recordId);

    /**
     * 保存带会话ID的用户问题
     *
     * @param chatRecords 聊天记录对象
     * @return 插入记录的主键值
     */
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into chat_records (conversation_id, user_question, timestamp, create_time, is_deleted) values (#{conversationId}, #{userQuestion}, #{timestamp}, #{createTime}, #{isDeleted})")
    int saveUserQuestionWithConversation(ChatRecords chatRecords);

    /**
     * 查询指定会话的所有对话记录（非删除状态）
     *
     * @param conversationId 会话ID
     * @return 聊天记录列表
     */
    @Select("select * from chat_records where conversation_id = #{conversationId} and is_deleted = 0 order by create_time asc")
    List<ChatRecords> selectRecordsByConversationId(Long conversationId);

    /**
     * 分页查询指定会话的对话记录
     *
     * @param params 参数Map，包含conversationId、offset、limit、isDeleted等
     * @return 聊天记录列表
     */
    @Select("select * from chat_records where conversation_id = #{conversationId} and is_deleted = #{isDeleted} order by create_time desc limit #{offset}, #{limit}")
    List<ChatRecords> selectRecordsByConversationIdPaged(Map<String, Object> params);

    /**
     * 获取指定会话的对话记录总数
     *
     * @param conversationId 会话ID
     * @param isDeleted 是否已删除(0-未删除,1-已删除)
     * @return 记录总数
     */
    @Select("select count(*) from chat_records where conversation_id = #{conversationId} and is_deleted = #{isDeleted}")
    int countRecordsByConversationId(@Param("conversationId") Long conversationId, @Param("isDeleted") Integer isDeleted);

    /**
     * 逻辑删除聊天记录
     *
     * @param id 记录ID
     * @return 受影响的行数
     */
    @Update("update chat_records set is_deleted = 1 where id = #{id}")
    int deleteRecord(Long id);

    /**
     * 批量逻辑删除会话下的所有聊天记录
     *
     * @param conversationId 会话ID
     * @return 受影响的行数
     */
    @Update("update chat_records set is_deleted = 1 where conversation_id = #{conversationId}")
    int deleteRecordsByConversationId(Long conversationId);

    /**
     * 恢复已删除的聊天记录（将is_deleted设为0）
     *
     * @param id 记录ID
     * @return 受影响的行数
     */
    @Update("update chat_records set is_deleted = 0 where id = #{id}")
    int restoreRecord(Long id);
}
